from selenium.webdriver.common.by import By
from page.base_page import BasePage
from time import sleep


class SearchPage(BasePage):
    """搜索功能页面类"""
    url = "/index.php?m=dynamic&a=index"  # 搜索功能所在页面URL

    # 定位器
    field_locator = (By.NAME, "field")  # 搜索字段下拉框
    user_field_option_locator = (By.CSS_SELECTOR, "#field > option:nth-child(1)")  # 用户搜索选项
    search_input_locator = (By.NAME, "search")  # 搜索输入框
    search_btn_locator = (By.ID, "searchBtn")  # 搜索按钮

    def click_field(self):
        """点击搜索字段下拉框（展开选项）"""
        self.click(self.field_locator)
        sleep(2)

    def select_user_field(self):
        """选择“用户”搜索字段（从下拉框中选择第一个选项）"""
        self.click(self.user_field_option_locator)
        sleep(2)

    def input_search_content(self, content):
        """输入搜索内容"""
        self.send_keys(self.search_input_locator, content)
        sleep(2)

    def click_search_button(self):
        """点击搜索按钮执行搜索"""
        self.click(self.search_btn_locator)
        sleep(2)

    def perform_user_search(self, content=""):
        """封装完整用户搜索流程：展开字段下拉框→选择用户字段→点击搜索"""
        self.click_field()
        self.select_user_field()
